package com.wongoing.sys.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wongoing.base.PageResult;
import com.wongoing.sys.model.SysConfig;
import com.wongoing.sys.service.SysConfigService;
import com.wongoing.sys.viewmodel.JsonResult;
import com.wongoing.util.LogService;
import com.wongoing.util.ModelAndViewConstants;
import com.wongoing.util.PageUtil;
import com.wongoing.util.Status;

@Controller
@RequestMapping("/sys/sysConfigAction")
public class SysConfigAction extends BaseAction{

	private SysConfigService service;

	@Autowired
	public void setService(SysConfigService service) {
		this.service = service;
	}
	
	@RequestMapping(value="/toList")
	public ModelAndView tolist(Integer pageSize, Integer pageCurrent, String orderField, String orderDirection,String paramTypeName){
		ModelAndView mv=null;
		try{
			mv=new ModelAndView();
			PageResult<SysConfig> pageResult = PageUtil.pageSet(this.getClass(), pageSize, pageCurrent, orderField, orderDirection);
			//非通用判断条件
			pageResult.getParam().put("status", "0");
			//模糊查询判断
			List<Boolean> multiFuzzyQuery = new ArrayList<Boolean>();
			
			if(paramTypeName!=null&&!"".equals(paramTypeName)){
				pageResult.getParam().put("typeName", paramTypeName);
				pageResult.setOrderField("Id");
				pageResult.setOrderDirection("DESC");
				pageResult.getExt().put("paramTypeName", paramTypeName);
				multiFuzzyQuery.add(true);
			}else{
				multiFuzzyQuery.add(false);
			}
			if(PageUtil.multiFuzzyQuery(multiFuzzyQuery)){
				pageResult.setOrderField("Id");
				pageResult.setOrderDirection("ASC");
			}
		pageResult=this.service.getConfigListPageReuslt(pageResult);
		mv = new ModelAndView();
		mv.addObject(ModelAndViewConstants.PAGE_RESULT, pageResult);
		mv.setViewName(ModelAndViewConstants.SYSCONFIG_MAIN_VIEW);
		LogService.getInstance(this).debug("系统配置项分页数据列表获取成功："+ModelAndViewConstants.SYSCONFIG_MAIN_VIEW);
		}catch(Exception ex){
			LogService.getInstance(this).error("分页获取系统配置项列表失败：" + ex.getMessage(), ex);
			mv = new ModelAndView(ModelAndViewConstants.ERROR_VIEW);
		}
		return mv;
	}
	
	
	@RequestMapping(value="/toAdd")
	public ModelAndView toAdd(){
		ModelAndView mv = null;
		try {
			mv = new ModelAndView();
			mv.addObject(ModelAndViewConstants.OPERATION_KEY, ModelAndViewConstants.OPERATION_VALUE_ADD);
			mv.addObject(ModelAndViewConstants.TITLE_KEY, "添加系统配置项");
			mv.setViewName(ModelAndViewConstants.SYSCONFIG_EDIT_VIEW);
			LogService.getInstance(this).debug("进入系统配置项添加页："+ModelAndViewConstants.SYSCONFIG_EDIT_VIEW);
		} catch (Exception ex) {
			LogService.getInstance(this).error("系统配置项插入失败：" + ex.getMessage(), ex);
			mv = new ModelAndView(ModelAndViewConstants.ERROR_VIEW);
		}
		return mv;
	}
	 
	@RequestMapping(value="/toEdit")
	public ModelAndView toEdit(Integer id){
		ModelAndView mv = null;
		try {
			SysConfig serveType = this.service.getByPrimaryKey(id);
			mv = new ModelAndView();
			mv.addObject(ModelAndViewConstants.OPERATION_KEY,ModelAndViewConstants.OPERATION_VALUE_EDIT);
			mv.addObject(ModelAndViewConstants.SYSCONFIG_MODEL,serveType);
			mv.addObject(ModelAndViewConstants.TITLE_KEY, "编辑系统配置项");
			mv.setViewName(ModelAndViewConstants.SYSCONFIG_EDIT_VIEW);
			LogService.getInstance(this).debug("进入修改系统配置项编辑页："+ModelAndViewConstants.SYSCONFIG_EDIT_VIEW);
		} catch (Exception ex) {
			LogService.getInstance(this).error("系统配置项修改失败：" + ex.getMessage(), ex);
			mv = new ModelAndView(ModelAndViewConstants.ERROR_VIEW);
		}
		return mv;
	}
	
	@RequestMapping(value="/toUpdate")
	public Map<String, Object> toUpdate(){
			Map<String, Object> jsonResult = null;
			try {
				String tabid = tabid(ModelAndViewConstants.SYSCONFIG_SYS_ID);
				service.refleshConfigCach();
				String msg = "系统配置项更新成功";
				jsonResult = JsonResult.jsonReturn(Status.OK.getCode(), msg, tabid,false,"","");
				LogService.getInstance(this).debug("系统配置项已经更新");
			} catch (Exception ex) {
				String msg = "系统配置项更新失败";
				jsonResult = JsonResult.jsonReturnErr(msg);
				LogService.getInstance(this).error("系统配置项更新失败：" + ex.getMessage(), ex);
			}
			return jsonResult;	
		}
	
	@ResponseBody
	@RequestMapping(value = "/doDel")
	public Map<String, Object> toDel(Integer id){
		Map<String, Object> jsonResult = null;
		try {
			SysConfig serveType  = new SysConfig();
			serveType.setId(id);
			serveType.setStatus("1");
			Integer count = this.service.updateByPrimaryKey(serveType);
			String tabid = tabid(ModelAndViewConstants.SYSCONFIG_SYS_ID);
			if(count>0){
				String msg = "系统配置项删除成功";
				jsonResult = JsonResult.jsonReturn(Status.OK.getCode(), msg, tabid,false,"","");
				LogService.getInstance(this).debug("删除系统配置项成功，数量："+count);
			}else{
				String msg = "该数据已删除，请核对数据正确性，或重新刷新数据尝试。";
				jsonResult = JsonResult.jsonReturnErr(msg);
				LogService.getInstance(this).debug("删除系统配置项失败，数量："+count);
			}
		} catch (Exception ex) {
			String msg = "删除失败";
			jsonResult = JsonResult.jsonReturnErr(msg);
			LogService.getInstance(this).error("系统配置项删除失败：" + ex.getMessage(), ex);
		}
		return jsonResult;	
	}
	
	
	@ResponseBody
	@RequestMapping(value="/doSave")
	public Map<String,Object> doSave(SysConfig serveType, String op){
		Map<String, Object> jsonResult = null;
		try {
			String tabid = tabid(ModelAndViewConstants.SYSCONFIG_SYS_ID);
			if (null == op || ModelAndViewConstants.OPERATION_VALUE_ADD.equals(op)) {
				serveType.setStatus("0");
				this.service.insert(serveType);
				Integer statusCode = 200;
				String msg = "系统配置项添加成功";
				jsonResult = JsonResult.jsonReturn(statusCode.intValue(), msg, tabid);
				LogService.getInstance(this).debug("添加系统配置项成功!");
			} else {
				this.service.updateByPrimaryKey(serveType);			
				Integer statusCode = 200;
				String msg = "系统配置项修改信息成功";
				jsonResult = JsonResult.jsonReturn(statusCode.intValue(), msg, tabid);
				LogService.getInstance(this).debug("编辑系统配置项信息成功!");

			}
		} catch (Exception e) {
			String msg = "保存系统配置项信息失败"+e.getMessage();
			jsonResult = JsonResult.jsonReturnErr(msg);
			LogService.getInstance(this).debug("保存系统配置项信息失败!"+e.getMessage());
		}
		return jsonResult;	
	}
}
